package nacos_zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import nacos_zuul.fegin.NacosAuthoaFegin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;

/**
 * @author A夜兔A
 * @data 2022年04月10日 18:01
 * 网关过滤
 */
@Component
public class NacosZuulFilter extends ZuulFilter {

    @Autowired
    private NacosAuthoaFegin nacosAuthoaFegin;
    /**
     * 执行的类型
     * pre 转发之前进行拦截
     * routing 转发时拦截
     * post 转发以后，请求完微服务回来进行拦截
     * error 报异常拦截
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 执行顺序：数值越小，越优先执行
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 执行条件
     * true 开启
     * false 关闭
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 执行逻辑
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        System.out.println("网关执行过滤");
        //获取当前请求的上下文
        RequestContext requestContext = RequestContext.getCurrentContext();
        //获取请求对象
        HttpServletRequest request = requestContext.getRequest();
        //获得session
        HttpSession session = request.getSession();
        System.out.println("session:"+session.toString());
        Principal userPrincipal = request.getUserPrincipal();
        System.out.println("userPrincipal"+userPrincipal);
        StringBuffer requestURL = request.getRequestURL();
        System.out.println("requestURL"+requestURL);
        String requestURI = request.getRequestURI();
        System.out.println("requestURI"+requestURI);

        //获得请求头参数
        String token = request.getParameter("token");
        String headerToken = request.getHeader("token");
        if (StringUtils.isEmpty(token)&&StringUtils.isEmpty(headerToken)){

            if (!"/nacos-authoa/login".equals(requestURI)){

                //通知ZuulApi当前请求违法
                requestContext.setSendZuulResponse(false);
                //设置响应状态
                requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
                //设置响应类型，两种方式
//            requestContext.addZuulResponseHeader("content-type","text/html;charset=utf-8");
                requestContext.getResponse().setContentType("application/json;charset=utf-8");

//            PrintWriter writer=null;
//            try {
//                writer = requestContext.getResponse().getWriter();
//                //设置响应内容
//                writer.print("好麻烦啊这个方法，用下面这个吧");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }finally {
//                writer.close();
//            }

                requestContext.setResponseBody("非法访问");
            }




        }else {
            System.out.println("用户携带令牌，验证令牌");

            String permitted = nacosAuthoaFegin.isPermitted(requestURI, headerToken);
            System.out.println(permitted);
            if ("false".equals(permitted)){
                requestContext.setSendZuulResponse(false);
                requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
                requestContext.getResponse().setContentType("application/json;charset=utf-8");
                requestContext.setResponseBody("没有权限");
            }


        }
        return null;
    }
}
